package ccmis.finance.capital.action;

import java.math.BigDecimal;
import java.util.List;
import javax.ejb.EJB;

import ccmis.finance.capital.bo.LendContractBO;
import ccmis.finance.capital.entity.LendContract;
import jfox.platform.infrastructure.SuperAction;
import org.jfox.framework.annotation.Service;
import org.jfox.mvc.ActionContext;
import org.jfox.mvc.Invocation;
import org.jfox.mvc.PageContext;
import org.jfox.mvc.annotation.ActionMethod;

/**
 * @author <a href="mailto:ren.yingb@gmail.com">Ren Yingbo</a>
 */
@Service(id="lendcontract")
public class LendContractAction extends SuperAction{
	
	@EJB
	LendContractBO lendContractBO;
    /**
     * 获取所有贷款合同
     * @param actionContext
     * @throws Exception
     */
	@ActionMethod(name = "listlendcontract",successView = "ccmis/finance/capital/lendcontract_list.vhtml")
    public void listLendContract(ActionContext actionContext) throws Exception{
		List<LendContract> lendContracts = lendContractBO.getAllLendContracts();
		PageContext pageContext = actionContext.getPageContext();
		pageContext.setAttribute("lendContracts", lendContracts);
	}
	
	/**
	 * 新增贷款合同
	 * @param actionContext
	 * @throws Exception
	 */
	@ActionMethod(name = "newview",successView = "ccmis/finance/capital/new_lendcontract.vhtml")
	public void newLendContractview(ActionContext actionContext) throws Exception{
		
	}
	
	/**
	 * 修改贷款合同
	 * @param actionContext
	 * @throws Exception
	 */
	@ActionMethod(name = "editview",successView = "ccmis/finance/capital/edit_lendcontract.vhtml",invocationClass = DelLendContractInvocation.class)
	public void editLendContractview(ActionContext actionContext) throws Exception{
    	DelLendContractInvocation invocation = (DelLendContractInvocation)actionContext.getInvocation();
        LendContract lendContract = lendContractBO.getLendContractById(invocation.getId());
        actionContext.getPageContext().setAttribute("lendContract", lendContract);
	}
	
	/**
	 * 保存新建的贷款合同
	 * @param actionContext
	 * @throws Exception
	 */
	@ActionMethod(name = "new",successView = "include/json_response.vhtml",errorView = "include/json_response.vhtml",invocationClass = CreateLendContractInvocation.class)
	public synchronized void createLendContract(ActionContext actionContext) throws Exception{
		CreateLendContractInvocation invocation = (CreateLendContractInvocation)actionContext.getInvocation();
		LendContract lendContract = new LendContract();
		lendContract.setLendDeptID(invocation.getLendDeptID());
		lendContract.setLendTypeID(invocation.getLendTypeID());
		lendContract.setCNo(invocation.getCNo());
		lendContract.setName(invocation.getName());
		lendContract.setPlanLendDate(jfox.platform.common.Utils.getMyLongDate(invocation.getPlanLendDate()));
		lendContract.setPlanReturnDate(jfox.platform.common.Utils.getMyLongDate(invocation.getPlanReturnDate()));
		lendContract.setYearLimit(invocation.getYearLimit());
		lendContract.setMonthLimit(invocation.getMonthLimit());
		lendContract.setCValue(invocation.getCValue());
		lendContract.setVstatus(invocation.getVstatus());
		lendContract.setPriority(invocation.getPriority());
		lendContractBO.newLendContract(lendContract);
		
		actionContext.getPageContext().setAttribute(JSON_RESPONSE_OBJ, lendContract);
	}
	
	/**
	 * 保存修改的贷款合同
	 * @param actionContext
	 * @throws Exception
	 */
	@ActionMethod(name = "edit",successView = "include/json_response.vhtml",errorView = "include/json_response.vhtml",invocationClass = EditLendContractInvocation.class)
	public synchronized void editLendContract(ActionContext actionContext) throws Exception{
		EditLendContractInvocation invocation = (EditLendContractInvocation)actionContext.getInvocation();
		LendContract lendContract = lendContractBO.getLendContractById(invocation.getId());
		System.out.println("ID=" + invocation.getId());
		System.out.println("lendContract" + lendContract);
		lendContract.setCNo(invocation.getCNo());
		lendContract.setName(invocation.getName());
		lendContract.setLendDeptID(invocation.getLendDeptID());
		lendContract.setLendTypeID(invocation.getLendTypeID());
		lendContract.setPlanLendDate(jfox.platform.common.Utils.getMyLongDate(invocation.getPlanLendDate()));
		lendContract.setPlanReturnDate(jfox.platform.common.Utils.getMyLongDate(invocation.getPlanReturnDate()));
		lendContract.setYearLimit(invocation.getYearLimit());
		lendContract.setMonthLimit(invocation.getMonthLimit());
		lendContract.setCValue(invocation.getCValue());
		lendContract.setVstatus(invocation.getVstatus());
		lendContract.setPriority(invocation.getPriority());
		lendContractBO.updateLendContract(lendContract);
		
		actionContext.getPageContext().setAttribute(JSON_RESPONSE_OBJ, lendContract);
	}
	
	/**
	 * 删除贷款合同
	 * @param actionContext
	 * @throws Exception
	 */
	@ActionMethod(name = "delete",successView = "include/json_response.vhtml",errorView = "include/json_response.vhtml",invocationClass = DelLendContractInvocation.class)
	public synchronized void deleteLendContract(ActionContext actionContext) throws Exception{
		DelLendContractInvocation invocation = (DelLendContractInvocation)actionContext.getInvocation();
		actionContext.getPageContext().setAttribute(JSON_RESPONSE_OBJ,invocation.getId());
		lendContractBO.deleteLendContract(invocation.getId());
	}
	
	public static class DelLendContractInvocation extends Invocation{
		private long id;

		public long getId() {
			return id;
		}

		public void setId(long id) {
			this.id = id;
		}
	}
	public static class EditLendContractInvocation extends CreateLendContractInvocation{
		private long id;

		public long getId() {
			return id;
		}

		public void setId(long id) {
			this.id = id;
		}
	}
	public static class CreateLendContractInvocation extends Invocation{
		private long lendDeptID;
		private int lendTypeID;
		private String cNo;
		private String name;
		private String planLendDate;
		private String planReturnDate;
		private int yearLimit;
		private int monthLimit;
		private BigDecimal cValue;
    	private int vstatus;
    	private int priority;
    	
		public long getLendDeptID() {
			return lendDeptID;
		}
		public void setLendDeptID(long lendDeptID) {
			this.lendDeptID = lendDeptID;
		}
		public int getLendTypeID() {
			return lendTypeID;
		}
		public void setLendTypeID(int lendTypeID) {
			this.lendTypeID = lendTypeID;
		}
		public String getCNo() {
			return cNo;
		}
		public void setCNo(String no) {
			cNo = no;
		}
		public String getPlanLendDate() {
			return planLendDate;
		}
		public void setPlanLendDate(String planLendDate) {
			this.planLendDate = planLendDate;
		}
		public String getPlanReturnDate() {
			return planReturnDate;
		}
		public void setPlanReturnDate(String planReturnDate) {
			this.planReturnDate = planReturnDate;
		}
		public BigDecimal getCValue() {
			return cValue;
		}
		public void setCValue(BigDecimal value) {
			cValue = value;
		}
		public int getVstatus() {
			return vstatus;
		}
		public void setVstatus(int vstatus) {
			this.vstatus = vstatus;
		}
		public int getPriority() {
			return priority;
		}
		public void setPriority(int priority) {
			this.priority = priority;
		}
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		public int getYearLimit() {
			return yearLimit;
		}
		public void setYearLimit(int yearLimit) {
			this.yearLimit = yearLimit;
		}
		public int getMonthLimit() {
			return monthLimit;
		}
		public void setMonthLimit(int monthLimit) {
			this.monthLimit = monthLimit;
		}
		}
	
}
